package com.lagou.edu.service.impl;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;
import com.lagou.edu.bean.UploadResult;
import com.lagou.edu.conf.AliyunConf;
import com.lagou.edu.service.OssDemoService;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@Service
public class IossdemoService implements OssDemoService{


	@Autowired
	private AliyunConf aliyunConf;

	@Autowired
	private OSSClient ossClient;

	// 允许上传的格式
	private static final String[] IMAGE_TYPE = new String[]{".jpg", ".png", ".jpeg"};


	private static final Long FILE_SIZE = 3 * 1024 * 1024L;



	@Override
	public UploadResult upload(MultipartFile multipartFile) {
		// 校验图片格式
		boolean isLegal = false;
		for (String type : IMAGE_TYPE) {
			if (StringUtils.endsWithIgnoreCase(multipartFile.getOriginalFilename(), type)) {
				isLegal = true;
				break;
			}
		}
		UploadResult upLoadResult = new UploadResult();
		if (!isLegal) {
			upLoadResult.setStatus("校验图片不支持");
			return upLoadResult;
		}
		// 校验图片大小
		long size = multipartFile.getSize();
		if (size > FILE_SIZE) {
			upLoadResult.setStatus("图片过大,上限为：" + FILE_SIZE);
			return upLoadResult;
		}

		String fileName = multipartFile.getOriginalFilename();
		String filePath = getFilePath(fileName);
		try {
			ossClient.putObject(aliyunConf.getBucketName(), filePath, new ByteArrayInputStream(multipartFile.getBytes()));
		} catch (IOException e) {
			e.printStackTrace();
			// 上传失败
			upLoadResult.setStatus("error");
			return upLoadResult;
		}
		upLoadResult.setStatus("done");
		upLoadResult.setName(aliyunConf.getUrlPrefix() + filePath);
		upLoadResult.setUid(filePath);
		return upLoadResult;
	}


	@Override
	public byte[] download(String fileName) throws IOException {
		OSSObject ossObject = ossClient.getObject(aliyunConf.getBucketName(), fileName);
		InputStream inputStream = ossObject.getObjectContent();
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		byte[] buff = new byte[1024];
		int rc = 0;
		while ((rc = inputStream.read(buff, 0, 1024)) > 0) {
			byteArrayOutputStream.write(buff, 0, rc);
		}
		inputStream.close();
		return byteArrayOutputStream.toByteArray();
	}

	@Override
	public UploadResult delete(String fileName) {
		ossClient.deleteObject(aliyunConf.getBucketName(), fileName);
		UploadResult upLoadResult = new UploadResult();
		upLoadResult.setStatus("删除");
		return upLoadResult;
	}


	// 生成不重复的文件路径和文件名
	private String getFilePath(String sourceFileName) {
		DateTime dateTime = new DateTime();
		return "images/" + dateTime.toString("yyyy")
				+ "/" + dateTime.toString("MM") + "/"
				+ dateTime.toString("dd") + "/" + UUID.randomUUID().toString() + "." +
				StringUtils.substringAfterLast(sourceFileName, ".");
	}
}
